﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WTwitter.ViewModel;
using WTwitter.Model.UserData;
using WTwitter.View.Config;
using System.Xml.Serialization;
using System.IO;

namespace WTwitter.View {
	/// <summary>
	/// MainWindow.xaml の相互作用ロジック
	/// </summary>
	public partial class MainWindow : Window {
		private string _configFileName = "MainWindowConfig.xml";
		private WindowConfig _config;

		public MainWindow(MainWindowViewModel viewModel) {
			if (File.Exists(_configFileName)) {
				var serializer = new XmlSerializer(typeof(WindowConfig));
				using (var stream = new FileStream(_configFileName, FileMode.Open)) {
					_config = serializer.Deserialize(stream) as WindowConfig;
				}
				this.Top = _config.Top;
				this.Left = _config.Left;
				this.Height = _config.Height;
				this.Width = _config.Width;
				this.Topmost = _config.IsTopMost;
			} else {
				_config = new WindowConfig();
				this.Height=500;
				this.Width = 400;
			}

			this.DataContext = viewModel;
			InitializeComponent();
		}

		protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
			_config.Top = this.Top;
			_config.Left = this.Left;
			_config.Height = this.Height;
			_config.Width = this.Width;
			_config.IsTopMost = this.Topmost;
			XmlSerializer serializer = new XmlSerializer(typeof(WindowConfig));
			using (var stream = new FileStream(_configFileName, FileMode.Create)) {
				serializer.Serialize(stream, _config);
			}

			base.OnClosing(e);
		}

		private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
			DragMove();
		}

	}
}
